import urllib
import urllib.request


# 访问指定url，保存至指定名称

# timeout单位是秒
def openurl(url, savePath='./data/test', saveName='test.html', timeout=2):
    maxTryNum = 3
    PATH_SAVE_NAME = savePath + '/' + saveName
    print("openurl2 " + url)
    head = {
        "User-Agent":
            # "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Mobile Safari/537.36"
            #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"
        # "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.36"
    }
    req = urllib.request.Request(url=url, headers=head)
    html = ""
    for tries in range(maxTryNum):
        try:
            response = urllib.request.urlopen(req, timeout=timeout)
            html = response.read().decode('utf-8')
            saveName = f'{saveName}'
            # print('get html success!')
            try:
                file = open(PATH_SAVE_NAME, 'w', encoding='utf-8')
                file.write(str(html))
                file.close()
                print(f'save {saveName} done!')
            except:
                print('save error')
            return html
        except:
            print(f"{saveName} error!")
            if tries < (maxTryNum - 1):
                continue
            else:
                print("Has tried %d times to access bid_url %s, all failed!" % (maxTryNum, url))
                return ""


if __name__ == '__main__':
    # openurl('http://www.taskcity.com//users/77858-bestpartner',saveName='user.html')
    openurl(' https://www.taskcity.com/users/34235/feedback')
